import openpyxl
from wordcloud import WordCloud


# 读取Excel表格数据
wb = openpyxl.load_workbook('data.xlsx')
sheet_names = wb.sheetnames

# 获取工作表
ws = wb['国内疫情']
frequency_out = {}
for each in sheet_names:
    if '洲' in each:
        ws = wb[each]
        for row in ws.values:
            if row[1] == "累计确诊":
                pass
            else:
                frequency_out[row[0]] = float(row[1])
    else:
        pass


# 以省份的确诊病例总数代表其出现的频率
frequency_in = {}
ws = wb['国内疫情']
for row in ws.values:
    if row[1] == "累计确诊":
        pass
    else:
        frequency_in[row[0]] = float(row[1])

# 定义调用方法生成文件
def generate_pic(frequency , name):
    # 设定图片规格
    wordcloud = WordCloud(font_path="C:/Windows/Fonts/FZYTK.TTF",
                          background_color="white",
                          width=1920, height=1080)
    # 根据累计病例数目生成词云
    wordcloud.generate_from_frequencies(frequency)

    # 保存文件
    wordcloud.to_file('%s.png'%(name))

generate_pic(frequency_in,'国内疫情词云图')
generate_pic(frequency_out,'国外疫情词云图')